var express = require("express");
// var jwt = require("jsonwebtoken");
// const { CLIENT_LONG_PASSWORD } = require("mysql/lib/protocol/constants/client");
const myroute = express.Router();
const db = require("../model/index");

myroute.use(express.json());
myroute.use(express.urlencoded({ extended: true }));

myroute.get("/listGet", function (req, res) {
  const sql = `SELECT * FROM message_type a LEFT JOIN message b on a.m_id = b.theme ORDER BY b.id`;
  const sql2 = `SELECT * FROM admin RIGHT JOIN application ON admin.work_id=application.apply_name LEFT 
  JOIN postadmin ON admin.jobs=postadmin.post_id LEFT JOIN department ON department.id=postadmin.post_department 
  LEFT JOIN application_type ON application_type.id=application.type WHERE  superior=${req.query.name}  ORDER BY application.id ;`;
  const sql3 = `SELECT * FROM admin RIGHT JOIN meeting ON admin.work_id=meeting.meeting_per LEFT JOIN
   postadmin ON admin.jobs=postadmin.post_id LEFT JOIN meetingroom ON meeting.meeting_room=meetingroom.metroom_id 
   LEFT JOIN department ON department.id=postadmin.post_department WHERE superior=${req.query.name} ORDER BY meeting_id;
  `;
  let resArr = [];
  let resArr2 = [];
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      db.query(sql2, (err2, data2) => {
        if (err2) {
          res.send({ code: 0, err2: err2 });
        } else {
          db.query(sql3, (err3, data3) => {
            if (err3) {
              res.send({ code: 0, err3: err3 });
            } else {
              function render(data) {
                let res = [];
                for (let i = 0; i < data.length; i++) {
                  if (res.length) {
                    if (data[i].m_type == "审批" && data[i].theme == 1) {
                      res[0].children.push({
                        id: data[i].id,
                        content: data[i].content,
                        time: data[i].time,
                        type: data[i].type,
                        mun: data[i].msg_num,
                      });
                    }
                  } else {
                    if (data[i].m_type == "审批" && data[i].theme == 1) {
                      res[0] = {
                        m_id: data[i].m_id,
                        m_type: data[i].m_type,
                        img: data[i].img,
                        title: data[i].title,
                        children: [
                          {
                            id: data[i].id,
                            content: data[i].content,
                            time: data[i].time,
                            type: data[i].type,
                            mun: data[i].msg_num,
                          },
                        ],
                      };
                    }
                  }
                }
                resArr[0] = res[0];
              }
              render(data);

              function render2(data) {
                let res = [];
                for (let i = 0; i < data.length; i++) {
                  if (res.length) {
                    if (data[i].theme == 2 && data[i].m_type == "会议") {
                      res[0].children.push({
                        id: data[i].id,
                        content: data[i].content,
                        time: data[i].time,
                        type: data[i].type,
                        mun: data[i].msg_num,
                      });
                    }
                  } else {
                    if (data[i].theme == 2 && data[i].m_type == "会议") {
                      res[0] = {
                        m_id: data[i].m_id,
                        m_type: data[i].m_type,
                        img: data[i].img,
                        title: data[i].title,
                        children: [
                          {
                            id: data[i].id,
                            content: data[i].content,
                            time: data[i].time,
                            type: data[i].type,
                            mun: data[i].msg_num,
                          },
                        ],
                      };
                    }
                  }
                }
                resArr2[0] = res[0];
              }
              render2(data);
              res.send({
                code: 200,
                data: [
                  { resArr: resArr[0] },
                  { resArr2: resArr2[0] },
                  data2,
                  data3,
                ],
              });
            }
          });
        }
      });
    }
  });
});

// 修改为已读
myroute.post("/addRead", function (req, res) {
  const sql = `
  UPDATE message_type a LEFT JOIN message b on a.m_id = b.theme  SET type='已读'
    WHERE b.type = '未读' && a.m_type='${req.body.name}'`;
  db.query(sql, (err, data) => {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//获取审批的信息
myroute.get("/spGet", function (req, res) {
  let sql = `
  SELECT * FROM message_type a LEFT JOIN message b on a.m_id = b.theme 
  WHERE  a.m_type LIKE '%审批%'
  ORDER BY b.id`;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//获取会议的信息
myroute.get("/hyGet", function (req, res) {
  let sql = `
  SELECT * FROM message_type a LEFT JOIN message b on a.m_id = b.theme 
  WHERE  a.m_type LIKE '%会议%'
  ORDER BY b.id`;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 获取部门
myroute.get("/bumenGet", function (req, res) {
  let sql = `
  SELECT * FROM department `;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

// 点击编辑获取 Ajax 下拉框
myroute.get("/bumenArray", function (req, res) {
  const mysql = `SELECT a.post_name,b.department_name,a.post_time,a.post_id from postadmin a LEFT JOIN department b on a.post_department=b.id
                 WHERE b.department_name='${req.query.bumen}' `;
  db.query(mysql, function (err, data) {
    if (err) {
      res.send({ code: 1, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//查流水号
myroute.get("/lsGet", function (req, res) {
  let sql = `
  SELECT * FROM application `;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//添加消息
myroute.post("/addPost", function (req, res) {
  let a =
    req.body.name_id + "于" + req.body.applytiem + "发起一条转岗申请，请您处理";
  let sql = `INSERT INTO application VALUES(null,'${req.body.mun}',${req.body.type},'${req.body.state}',${req.body.audit},null,'${req.body.content}',null,'${req.body.applytiem}',null,${req.body.applyname},null,null,null,${req.body.bewjbos})`;
  let sql2 = `INSERT INTO message VALUES(null,'${req.body.name_id}','${a}','${req.body.applytiem}','未读',1,'${req.body.mun}')`;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      db.query(sql2, function (err2, data2) {
        if (err) {
          res.send({ code: 0, err: err2 });
        } else {
          res.send({ code: 200, data: [data, data2] });
        }
      });
    }
  });
});

//审批消息跳转
myroute.get("/jump", function (req, res) {
  let sql = `
  SELECT * FROM admin RIGHT JOIN application ON admin.work_id=application.apply_name LEFT 
      JOIN postadmin ON admin.jobs=postadmin.post_id LEFT JOIN department ON department.id=postadmin.post_department 
      LEFT JOIN application_type ON application_type.id=application.type WHERE mun='${req.query.num}'                           
       LIMIT 0, 1000;`;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});

//会议消息跳转
myroute.get("/meetingJump", function (req, res) {
  console.log(req.query);
  let sql = `
  SELECT * FROM admin RIGHT JOIN meeting ON admin.work_id=meeting.meeting_per LEFT JOIN postadmin
  ON admin.jobs=postadmin.post_id LEFT JOIN meetingroom ON meeting.meeting_room=meetingroom.metroom_id
   LEFT JOIN department ON department.id=postadmin.post_department WHERE meeting_num=${req.query.num}  ;`;
  db.query(sql, function (err, data) {
    if (err) {
      res.send({ code: 0, err: err });
    } else {
      res.send({ code: 200, data: data });
    }
  });
});
module.exports = myroute;
